package com.serkol.driving.learning.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.bean.Result;
import com.serkol.driving.learning.entity.Attribute;
import com.serkol.driving.learning.entity.AttributeValue;
import com.serkol.driving.learning.service.AttributeService;
import com.serkol.driving.learning.service.AttributeValueService;
import com.serkol.constant.ResultConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */

@RestController
@RequestMapping("api/cdn/attr")
public class AttributeController {
    @Autowired
    private AttributeService service;
    @Autowired
    private AttributeValueService valueService;


    @GetMapping("findByList")
    public Result list() {
        List<Attribute> list = service.list();
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);
    }



    @GetMapping("findBySelectList")
    public Result findBySelectList() {
        List<Map<String,Object>> list = service.findBySelectList();
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);
    }



    @PostMapping("edit")
    public Result edit(@RequestBody Attribute data) {



            boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {

                flag = service.updateById(data);
            } else {
                flag = service.save(data);
            }

            return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }


    @PostMapping("valueEdit")
    public Result valueEdit(@RequestBody AttributeValue data) {

        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {

            flag = valueService.updateById(data);
        } else {
            flag = valueService.save(data);
        }

        return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }



    @GetMapping("valueListByAttrId")
  //  @Cacheable(cacheNames = "valueListByAttrId", key = "#attrId")
    public Result valueListByAttrId( @RequestParam(value = "attrId") long attrId){

        QueryWrapper<AttributeValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(AttributeValue::getAttributeId,attrId);

        List<AttributeValue> list = valueService.list(queryWrapper);
        return !list.isEmpty()? Result.ok(list):Result.failure(ResultConstant.EMPTIY);
    }


    @PostMapping("attrByKey")
    public Result attrByKey( @RequestParam(value = "attrkey") String attrkey){
        List<AttributeValue> list = valueService.findByKey(attrkey);
        return !list.isEmpty()? Result.ok(list):Result.failure(ResultConstant.EMPTIY);
    }
    @GetMapping("findBySelectValueList")
    public Result findBySelectValueList( @RequestParam(value = "key") String attrkey,
                                         @RequestParam(value = "type",defaultValue="0") int type){
        if(StringUtils.isNotEmpty(attrkey)){
        if(type==1){
            List<Map<String, Object>> list = valueService.findBySelectValueKeyList(attrkey);
            return !list.isEmpty()? Result.ok(list):Result.failure(ResultConstant.EMPTIY);
        }else{
            Map<String,Map<String,Object>> list =valueService.findBySelectValueKey(attrkey);
            return !list.isEmpty()? Result.ok(list):Result.failure(ResultConstant.EMPTIY);
        }}else {
            return Result.failure();
        }



    }

}

